package com.yuanli.yuanliutils.yl_selectImg;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.media.MediaMetadataRetriever;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnPermissionDescriptionListener;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;
import com.luck.picture.lib.permissions.PermissionConfig;
import com.luck.picture.lib.utils.DensityUtil;
import com.luck.picture.lib.widget.MediumBoldTextView;
import com.yuanli.yuanliutils.R;

import java.util.ArrayList;

public  class PictureSelectorUtils {

    //写一个回调函数，回调ArrayList<LocalMedia> result
    public interface OnResultCallback {
        void onResult(ArrayList<LocalMedia> result);
    }


    /**
     * 选择图片
     * @param context 上下文
     * @param maxNum 最大选择数量
     * @param minNum 最小选择数量
     * @param selType 选择类型 SelectMimeType.ofImage()
     */
    public   static void selectPic(Context context, int maxNum, int minNum, int selType,OnResultCallback onResultCallback) {
        PictureSelector.create(context)
                .openGallery(selType)
                .setImageEngine(PicassoEngine.createPicassoEngine())
                .setMaxSelectNum(maxNum)
                .setMinSelectNum(minNum)
                .setPermissionDescriptionListener(new OnPermissionDescriptionListener() {
                    @Override
                    public void onPermissionDescription(Fragment fragment, String[] permissionArray) {
                        View rootView = fragment.requireView();
                        if (rootView instanceof ViewGroup) {
                            addPermissionDescription(false, (ViewGroup) rootView, permissionArray);
                        }
                    }

                    @Override
                    public void onDismiss(Fragment fragment) {
                        removePermissionDescription((ViewGroup) fragment.requireView());
                    }
                })
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(ArrayList<LocalMedia> result) {
                        onResultCallback.onResult(result);
                    }

                    @Override
                    public void onCancel() {

                    }
                });
    }

    /**
     * 添加权限说明
     *
     * @param viewGroup
     * @param permissionArray
     */
    private static void addPermissionDescription(boolean isHasSimpleXCamera, ViewGroup viewGroup, String[] permissionArray) {
        int dp10 = DensityUtil.dip2px(viewGroup.getContext(), 10);
        int dp15 = DensityUtil.dip2px(viewGroup.getContext(), 15);
        MediumBoldTextView view = new MediumBoldTextView(viewGroup.getContext());
        view.setTag("TAG_EXPLAIN_VIEW");
        view.setTextSize(14);
        view.setTextColor(Color.parseColor("#333333"));
        view.setPadding(dp10, dp15, dp10, dp15);

        String title;
        String explain;

        if (TextUtils.equals(permissionArray[0], PermissionConfig.CAMERA[0])) {
            title = "相机权限使用说明";
            explain = "相机权限使用说明\n用户app用于拍照/录视频";
        }
//        else if (TextUtils.equals(permissionArray[0], Manifest.permission.RECORD_AUDIO)) {
//            if (isHasSimpleXCamera) {
//                title = "麦克风权限使用说明";
//                explain = "麦克风权限使用说明\n用户app用于录视频时采集声音";
//            } else {
//                title = "录音权限使用说明";
//                explain = "录音权限使用说明\n用户app用于采集声音";
//            }
//        }
        else {
            title = "存储权限使用说明";
            explain = "存储权限使用说明\n用户app写入/下载/保存/读取/修改/删除图片、视频、文件等信息";
        }
        int startIndex = 0;
        int endOf = startIndex + title.length();
        SpannableStringBuilder builder = new SpannableStringBuilder(explain);
        builder.setSpan(new AbsoluteSizeSpan(DensityUtil.dip2px(viewGroup.getContext(), 16)), startIndex, endOf, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        builder.setSpan(new ForegroundColorSpan(0xFF333333), startIndex, endOf, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        view.setText(builder);
        view.setBackground(ContextCompat.getDrawable(viewGroup.getContext(), R.drawable.ps_dialog_shadow));

        if (isHasSimpleXCamera) {
            RelativeLayout.LayoutParams layoutParams =
                    new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.topMargin = DensityUtil.getStatusBarHeight(viewGroup.getContext());
            layoutParams.leftMargin = dp10;
            layoutParams.rightMargin = dp10;
            viewGroup.addView(view, layoutParams);
        } else {
            ConstraintLayout.LayoutParams layoutParams =
                    new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
//            layoutParams.topToBottom = R.id.title_bar;
            layoutParams.leftToLeft = ConstraintSet.PARENT_ID;
            layoutParams.leftMargin = dp10;
            layoutParams.rightMargin = dp10;
            viewGroup.addView(view, layoutParams);
        }
    }

    /**
     * 移除权限说明
     *
     * @param viewGroup
     */
    private static void removePermissionDescription(ViewGroup viewGroup) {
        View tagExplainView = viewGroup.findViewWithTag("TAG_EXPLAIN_VIEW");
        viewGroup.removeView(tagExplainView);
    }
}
